package com.bbva.gptf.back.dominio;

import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;

import oracle.sql.CLOB;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Configurable
public class SpFicheroSaldos {
    		
	private SimpleJdbcCall simpleJdbcCall;
    private DataSource dataSource;

    @Autowired
    public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
    }

    @PersistenceContext
    transient EntityManager entityManager;
	
	@Transactional
    public String execute(){
    	String procedureName = "PKG_GPT.SP_FICHERO_SALDOS";
        this.simpleJdbcCall = new SimpleJdbcCall(this.dataSource);
        this.simpleJdbcCall.withoutProcedureColumnMetaDataAccess();
        this.simpleJdbcCall.withProcedureName(procedureName);
        this.simpleJdbcCall.declareParameters(new SqlOutParameter("p_saldos", Types.CLOB));
        Map<String, Object> parametros = new HashMap<String, Object>();        
        Map<String, Object> out = this.simpleJdbcCall.execute(parametros);
        CLOB clob = (CLOB) out.get("p_saldos");
        String cadena = "";
        if (clob != null) {
        	 try {
				if ((int) clob.length() > 0) {
					 cadena = clob.getSubString(1, (int) clob.length());  
				 }
			} catch (SQLException e) {
				e.printStackTrace();
			}
        }
        return cadena;
	}
}
